package com.monkey_pig.memoryalbum.utils;

/**
 * Created by Junerver on 2016/5/26.
 */
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.provider.MediaStore;
import android.util.Log;
public final class BitmapUtils {



    public static Bitmap decodeBitmap(String path, int displayWidth, int displayHeight){
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
        //获取比例大小
        int wRatio = (int)Math.ceil(op.outWidth/(float)displayWidth);
        int hRatio = (int)Math.ceil(op.outHeight/(float)displayHeight);
        //如果超出指定大小，则缩小相应的比例
        if(wRatio > 1 && hRatio > 1){
            if(wRatio > hRatio){
                op.inSampleSize = wRatio;
            }else{
                op.inSampleSize = hRatio;
            }
        }
        op.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeFile(path, op);
        return Bitmap.createScaledBitmap(bmp, displayWidth, displayHeight, true);
    }

    /**
     * 采用复杂计算来决定缩放
     * @param path
     * @param maxImageSize
     * @return
     */
    public static Bitmap decodeBitmap(String path, int maxImageSize){
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
        int scale = 1;
        if(op.outWidth > maxImageSize || op.outHeight > maxImageSize){
            scale = (int)Math.pow(2, (int)Math.round(Math.log(maxImageSize/(double)Math.max(op.outWidth, op.outHeight))/Math.log(0.5)));
        }
        op.inJustDecodeBounds = false;
        op.inSampleSize = scale;
        bmp = BitmapFactory.decodeFile(path, op);
        return bmp;
    }


    public static Cursor queryThumbnails(Activity context){
        String[] columns = new String[]{
                MediaStore.Images.Thumbnails.DATA,
                MediaStore.Images.Thumbnails._ID,
                MediaStore.Images.Thumbnails.IMAGE_ID
        };
        return context.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns, null, null, MediaStore.Images.Thumbnails.DEFAULT_SORT_ORDER);
    }

    public static Cursor queryThumbnails(Activity context, String selection, String[] selectionArgs){
        String[] columns = new String[]{
                MediaStore.Images.Thumbnails.DATA,
                MediaStore.Images.Thumbnails._ID,
                MediaStore.Images.Thumbnails.IMAGE_ID
        };
        return context.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, MediaStore.Images.Thumbnails.DEFAULT_SORT_ORDER);
    }

    public static Bitmap queryThumbnailById(Activity context, int thumbId){
        String selection = MediaStore.Images.Thumbnails._ID + " = ?";
        String[] selectionArgs = new String[]{
                thumbId+""
        };
        Cursor cursor = BitmapUtils.queryThumbnails(context,selection,selectionArgs);

        if(cursor.moveToFirst()){
            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
            cursor.close();
            return BitmapUtils.decodeBitmap(path, 100, 100);
        }else{
            cursor.close();
            return null;
        }
    }

    public static Bitmap[] queryThumbnailsByIds(Activity context, Integer[] thumbIds){
        Bitmap[] bitmaps = new Bitmap[thumbIds.length];
        for(int i=0; i<bitmaps.length; i++){
            bitmaps[i] = BitmapUtils.queryThumbnailById(context, thumbIds[i]);
        }

        return bitmaps;
    }

    /**
     * 获取全部
     * @param context
     * @return
     */
    public static List<Bitmap> queryThumbnailList(Activity context){
        List<Bitmap> bitmaps = new ArrayList<Bitmap>();
        Cursor cursor = BitmapUtils.queryThumbnails(context);
        for(int i=0; i<cursor.getCount(); i++){
            cursor.moveToPosition(i);
            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
            Bitmap b = BitmapUtils.decodeBitmap(path, 100, 100);
            bitmaps.add(b);
        }
        cursor.close();
        return bitmaps;
    }

    public static List<Bitmap> queryThumbnailListByIds(Activity context, int[] thumbIds){
        List<Bitmap> bitmaps = new ArrayList<Bitmap>();
        for(int i=0; i<thumbIds.length; i++){
            Bitmap b = BitmapUtils.queryThumbnailById(context, thumbIds[i]);
            bitmaps.add(b);
        }

        return bitmaps;
    }

    public static Cursor queryImages(Activity context){
        String[] columns = new String[]{
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME
        };
        return context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
    }

    public static Cursor queryImages(Activity context, String selection, String[] selectionArgs){
        String[] columns = new String[]{
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME
        };
        return context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
    }

    public static Bitmap queryImageById(Activity context, int imageId){
        String selection = MediaStore.Images.Media._ID + "=?";
        String[] selectionArgs = new String[]{
                imageId + ""
        };
        Cursor cursor = BitmapUtils.queryImages(context, selection, selectionArgs);
        if(cursor.moveToFirst()){
            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
            cursor.close();
            //return BitmapUtils.decodeBitmap(path, 260, 260);
            //这里会限制图片的输出尺寸
            return BitmapUtils.decodeBitmap(path, 500); //看看和上面这种方式的差别,看了，差不多
        }else{
            cursor.close();
            return null;
        }
    }

    /**
     * 根据缩略图的Id获取对应的大图
     * @param context
     * @param thumbId
     * @return
     */
    public static Bitmap queryImageByThumbnailId(Activity context, Integer thumbId){

        String selection = MediaStore.Images.Thumbnails._ID + " = ?";
        String[] selectionArgs = new String[]{
                thumbId+""
        };
        Cursor cursor = BitmapUtils.queryThumbnails(context, selection, selectionArgs);

        if(cursor.moveToFirst()){
            int imageId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
            cursor.close();
            return BitmapUtils.queryImageById(context, imageId);
        }else{
            cursor.close();
            return null;
        }
    }

    public static String queryImagePathByThumbnailId(Activity context, Integer thumbId) {
        //两次查询，先通过缩略图id查询出原图id，再通过原图ID查询出原图的路径
        String selection = MediaStore.Images.Thumbnails._ID + " = ?";
        String[] selectionArgs = new String[]{
                thumbId+""
        };
        Cursor cursor = BitmapUtils.queryThumbnails(context, selection, selectionArgs);

        if(cursor.moveToFirst()){
            //查询原图id
            int imageId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));

            String selection_1 = MediaStore.Images.Media._ID + "=?";
            String[] selectionArgs_1 = new String[]{
                    imageId + ""
            };
            //获取原图路径
            cursor = BitmapUtils.queryImages(context, selection_1, selectionArgs_1);
            if(cursor.moveToFirst()){
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                cursor.close();
                return path;
            }else{
                cursor.close();
                return null;
            }
        }else{
            cursor.close();
            return null;
        }

    }

}